package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.entity.SystemParameterDo;
import com.shop.mapper.SystemParameterDoMapper;
import com.shop.service.SystemParameterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shopEnum.SystemParameterEnum;
import com.shop.vo.GetSystemParameterVo;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统参数表 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2020-12-17
 */
@Service
public class SystemParameterServiceImpl extends ServiceImpl<SystemParameterDoMapper, SystemParameterDo> implements SystemParameterService {

    @Override
    public ResultModel getSystemParameter(GetSystemParameterVo getSystemParameterVo) throws Exception {
        List<SystemParameterDo> systemParameterDos = this.selectSystemParameterByType(getSystemParameterVo.getSystemType());
        Collections.sort(systemParameterDos, (s1, s2) -> s1.getSystemSort().compareTo(s2.getSystemSort()));
        Map<Integer, List<SystemParameterDo>> map = systemParameterDos.stream().collect(Collectors.groupingBy(SystemParameterDo::getSystemType));
        return Result.successResultModel(map);
    }

    @Override
    public List<SystemParameterDo> selectSystemParameterByType(Integer type) throws Exception {
        QueryWrapper<SystemParameterDo> systemParameterDoQueryWrapper = new QueryWrapper<>();
        systemParameterDoQueryWrapper.eq("is_del", "N");
        if (type != null)
            systemParameterDoQueryWrapper.eq("system_type", type);
        return super.list(systemParameterDoQueryWrapper);
    }
}
